package com.ruoyi.aitcommon.service;

import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.aitcommon.config.TaskConfig;
import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.util.ScheduleUtils;

/**
 * 调度任务服务
 * @author Administrator
 *
 */
@Service
public class TaskService {
	@Autowired
	private TaskConfig taskConfig;
	@Autowired
    private Scheduler scheduler;

	/**
	 * 手工触发,只运行一次
	 * 依据taskid及时运行任务,单独线程
	 * @param taskid
	 * @throws Exception
	 */
	public void run(Long taskid) throws Exception {
        SysJob job = taskConfig.getJob(taskid);
        if(job==null)
        	throw new Exception("任务:"+taskid.toString()+" 查找失败!");
        
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, job);
//        立即执行任务
        scheduler.triggerJob(ScheduleUtils.getJobKey(job), dataMap);
	}
	
	/**
	 * 暂停
	 * @param taskid
	 * @throws Exception
	 */
	public void pause(Long taskid) throws Exception {
		SysJob job = taskConfig.getJob(taskid);
        if(job==null)
        	throw new Exception("任务["+taskid.toString()+"]查找失败!");

        scheduler.pauseJob(ScheduleUtils.getJobKey(job));
	}
	
	/**
	 * 恢复
	 * @param taskid
	 * @throws Exception
	 */
	public void resume(Long taskid) throws Exception {
		SysJob job = taskConfig.getJob(taskid);
        if(job==null)
        	throw new Exception("任务["+taskid.toString()+"]查找失败!");

        scheduler.resumeJob(ScheduleUtils.getJobKey(job));
	}
}
